package top.healthylife.gateway.core.util;

import top.healthylife.basics.utils.exception.BusinessException;
import top.healthylife.basics.utils.utils.SpringContextUtil;
import top.healthylife.basics.utils.utils.SystemThreadLocal;
import top.healthylife.gateway.core.config.SystemInit;
import top.healthylife.gateway.core.service.ParentFace;

/**
 * @author maoxiaomeng
 */
public class SystemApiUtil extends SystemThreadLocal {

    static final String SERVICE_NAME = "service";

    /**
     * 获取要调用的接口
     *
     * @return
     */
    public static ParentFace get() {
        return get(SERVICE_NAME);
    }

    /**
     * 将当前请求的接口方法存放到当前线程中
     *
     * @param apiName 接口请求名称
     * @throws Exception
     */
    public static void set(String apiName) throws BusinessException {
        //根据API名称找到对象的BEAN，并存放到当前线程中去
        if (SystemInit.getApiClassMap().containsKey(apiName)) {
            set(SERVICE_NAME, SpringContextUtil.getBean(SystemInit.getApiClassMap().get(apiName)));
        } else {
            throw new BusinessException("请核对后再调用,未找到对应的接口:" + apiName);
        }
    }

}
